﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using GameControls;
using GameUtils.Input;
using ThouShaltNotBrick.Models;
using ThouShaltNotBrick.Logic.GeneralObjects;
using Microsoft.Xna.Framework.Graphics;
using ThouShaltNotBrick.Logic.Helpers;

namespace ThouShaltNotBrick.Logic.GameStates
{
    internal sealed class SelectCampaignGameState : GameStateBase
    {
        #region Properties
        private Label mSelectCampaignLabel;
        private ScrollView mScrollView;
        private List<Button> mCampaignButtons;

        private BrickStatesAndTypes mBrickStatesAndTypes;
        #endregion

        #region Overriden Methods
        protected override void InitializeImpl()
        {
            mClearColor = Color.Orange;
            mCampaignButtons = new List<Button>();

            mBrickStatesAndTypes = new BrickStatesAndTypes(mGameResources, mGraphicsResources);

            InitializeControls();
        }

        protected override void DestroyImpl()
        {
            DestroyControls();
        }

        protected override void StartImpl(GameStateChangeData pChangeData)
        {
        }

        protected override void StopImpl()
        {
        }

        protected override void ResumeImpl(GameStateChangeData pChangeData)
        {
        }

        protected override void PauseImpl()
        {
        }

        protected override void HandleInputImpl(GameTime pGameTime)
        {
        }

        protected override void UpdateImpl(GameTime pGameTime)
        {
        }

        protected override void DrawImpl(GameTime pGameTime)
        {
        }

        #region Keyboard
        protected override void OnKeyDownImpl(KeyEventArgs pArgs)
        {
            if (pArgs.Key == Keys.Escape)
            {
                GameStateManager.PopState();
            }
        }
        #endregion
        #endregion

        #region Methods
        private void InitializeControls()
        {
            DestroyControls();

            SpriteFont selectCampaignFont = mFontResources.Font28;
            int selectCampaignLabelHeight = (int)selectCampaignFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle selectCampaignLabelRectangle = new Rectangle(
                0,
                0,
                mGameManager.GetClientWidth(),
                selectCampaignLabelHeight);
            mSelectCampaignLabel = new Label(selectCampaignLabelRectangle);
            mSelectCampaignLabel.SetText("Select Campaign", selectCampaignFont, Color.Black);
            mSelectCampaignLabel.HorizontalAlignment = HorizontalAlignment.Center;
            mSelectCampaignLabel.VerticalAlignment = VerticalAlignment.Top;
            mControls.Add(mSelectCampaignLabel);

            Rectangle scrollViewRect = new Rectangle(
                0,
                selectCampaignLabelHeight,
                mGameManager.GetClientWidth(),
                mGameManager.GetClientHeight() - selectCampaignLabelHeight);
            mScrollView = new ScrollView(scrollViewRect, mGraphicsResources.WhiteTexture);
            mControls.Add(mScrollView);

            CampaignsData campaignsData = mGameResources.CampaignsData;
            List<string> campaigns = campaignsData.GetCampaigns();

            SpriteFont campaignFont = mFontResources.Font20;
            int campaignLabelHeight = (int)campaignFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            int scrollViewClientWidth = scrollViewRect.Width - ScrollView.SLIDER_WIDTH;
            for (int i = 0; i < campaigns.Count; i++)
            {
                Rectangle campaignRect = new Rectangle(0, i * campaignLabelHeight, scrollViewClientWidth, campaignLabelHeight);
                Button campaignButton = new Button(campaignRect, mGraphicsResources.WhiteTexture);
                campaignButton.Label.SetText(campaigns[i], campaignFont, Color.Black);
                campaignButton.Pressed += CampaignButtonPressedHandler;
                mCampaignButtons.Add(campaignButton);
                mScrollView.AddChild(campaignButton);
            }
        }

        private void DestroyControls()
        {
            foreach (Button campaignButton in mCampaignButtons)
            {
                campaignButton.Pressed -= CampaignButtonPressedHandler;
            }
            mCampaignButtons.Clear();

            mControls.Clear();
        }

        private PlayData CreatePlayData(string pCampaignName)
        {
            Campaign campaign = new Campaign(pCampaignName, mGameResources, mBrickStatesAndTypes);
            PlayData playData = new PlayData(campaign);

            return playData;
        }
        #endregion

        #region Event Handlers
        private void CampaignButtonPressedHandler(object sender, EventArgs e)
        {
            Button campaignButton = sender as Button;
            if (campaignButton != null)
            {
                PlayData playData = CreatePlayData(campaignButton.Label.Text);
                GameStateChangeData gscd = new GameStateChangeData(playData);
                GameStateManager.ChangeState(GameStateType.PlayStart, gscd);
            }
        }
        #endregion
    }
}